package com.neusoft.bizcore.activiti.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.activiti.dto.ProcessFormDTO;
import com.neusoft.bizcore.activiti.dto.ProcessFormItemDTO;
import com.neusoft.bizcore.activiti.service.ProcessFormItemService;
import com.neusoft.bizcore.activiti.service.ProcessFormService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "工作流表单管理")
@RestController
@RequestMapping(value = "/api/w/act/forms")
public class ActFormController {

    @Autowired
    private ProcessFormService processFormService;
    @Autowired
    private ProcessFormItemService processFormItemService;

    @ApiOperation("查询所有表单")
    @GetMapping
    public ResultListDTO<ProcessFormDTO> index(final Searchable searchable) {
        final List<ProcessFormDTO> DTOs = this.processFormService.index(searchable);
        final ResultListDTO<ProcessFormDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有表单")
    @GetMapping("/p")
    public PageResultDTO<ProcessFormDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.processFormService.search(pageable, searchable));
    }

    @ApiOperation("查询所有字段")
    @GetMapping("/field")
    public ResultListDTO<ProcessFormItemDTO> fieldIndex(final Searchable searchable) {
        final List<ProcessFormItemDTO> DTOs = this.processFormItemService.index(searchable);
        final ResultListDTO<ProcessFormItemDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有字段")
    @GetMapping("/field/p")
    public PageResultDTO<ProcessFormItemDTO> fieldSearch(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.processFormItemService.search(pageable, searchable));
    }

    @ApiOperation("创建表单")
    @PostMapping("")
    public ResultDTO<ProcessFormDTO> create(@RequestBody final ProcessFormDTO dto) {
        final ProcessFormDTO result = this.processFormService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改表单")
    @PutMapping("/{id}")
    public ResultDTO<ProcessFormDTO> update(@PathVariable final Long id,
            @RequestBody final ProcessFormDTO dto) {
        final ProcessFormDTO result = this.processFormService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("表单详情")
    @GetMapping("/{id}")
    public ResultDTO<ProcessFormDTO> details(@PathVariable final Long id) {
        final ProcessFormDTO dto = this.processFormService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除表单")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.processFormService.delete(id);
        return ResultDTO.success();
    }
}
